package com.me.hippy.sdk.demo.chat

import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.ViewGroup
import android.widget.EditText
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.RelativeLayout
import androidx.fragment.app.FragmentActivity
import com.me.hippy.sdk.R

class AiliaoChatActivity2 : FragmentActivity(), KeyboardChangeListener.C1117a {
    private lateinit var toolPanel: FrameLayout
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val activity = this
        var contentViewGroup = window.decorView as ViewGroup
        for (i in 0 until contentViewGroup.childCount) {
            val childAt = contentViewGroup.getChildAt(i)
            if (childAt is LinearLayout) {
                contentViewGroup = childAt
                break
            }
        }
        for (i in 0 until contentViewGroup.childCount) {
            val childAt = contentViewGroup.getChildAt(i)
            if (childAt is FrameLayout) {
                contentViewGroup = childAt
                break
            }
        }

        val rootView = RelativeLayout(activity)
        contentViewGroup.addView(rootView)
        rootView.layoutParams = FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.MATCH_PARENT
        )
        rootView.setBackgroundResource(R.drawable.ms_call_bg)

        val editTextPanel = LinearLayout(activity)
        rootView.addView(editTextPanel)
        editTextPanel.orientation = LinearLayout.VERTICAL
        val layoutParams = RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
        )
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)
        editTextPanel.layoutParams = layoutParams

        val editText = EditText(activity)
        editTextPanel.addView(
            editText,
            LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )
        )

        toolPanel = FrameLayout(activity)
        editTextPanel.addView(
            toolPanel,
            LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )
        )

        val keyboardChangeListener = KeyboardChangeListener(this)
        keyboardChangeListener.mo14613a(this)
        window.setSoftInputMode(19)

        enableTranslucentStatusBarEffects()
//        setContentView(rootView)
    }

    override fun mo14615a(z: Boolean, i: Int) {
        Log.e("=======", "z:$z , i=$i")
        if (z) {
            val layoutParams = toolPanel.layoutParams
            layoutParams.height = i
            toolPanel.layoutParams = layoutParams
        } else {
            val layoutParams = toolPanel.layoutParams
            layoutParams.height = 0
            toolPanel.layoutParams = layoutParams
        }
    }

    fun enableTranslucentStatusBarEffects() {
        if (Build.VERSION.SDK_INT >= 19) {
            val window = window
            val attributes = window.attributes
            attributes.flags = attributes.flags or 67108864
            window.attributes = attributes
        }
    }
}